<?php
/*
 * 品牌静态类
 * @package    auto
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 36ce869e7652a3b4c1a455d5a005fa2fc59b4532 $
 */
defined('IN_IXDCW') or exit('Access Denied');
/**
 * 品牌静态类
 * Class AutoBrand
 */
class AutoBrand {

	private static $_tableCategory;
	private static $_tableBrand;

	/**
	 * 获取品牌信息
	 * @param $brandid
	 * @return mixed
	 */
	public static function get($brandid) {
		global $db;
		$brand = array();
		$table = Table::name(Table::NAME_AUTO_RANK_BRAND);
		$RANK  = AutoCategory::getCache(AutoCategory::TYPE_RANK);

		foreach ($RANK as $rank) {
			if ($rank['level'] != 0) {
				continue;
			}
			if ($rank['arid'] == $brandid) {
				$brand = $rank;
			}
		}
		if (!$brand) {
			return $brand;
		} else {
			$sql             = "select arid,brand_thumb as thumb,description from $table where arid={$brand['arid']}";
			$auto_rank_Brand = $db->getOne($sql);
			$brand           = array_merge($brand, $auto_rank_Brand);
			return $brand;
		}
	}

	/**
	 * 获取简短的品牌信息
	 * @param $brandId
	 * @return array|null
	 */
	public static function getShort($brandId) {
		global $db;
		$brandIdin = is_numeric($brandId) ? intval($brandId) : implode(',', $brandId);
		$sql       = "select * from  " . self::_getTableCategory() . " where arid in($brandIdin)";
		if (is_numeric($brandId)) {
			return $db->getOne($sql);
		} else {
			return $db->getAll($sql);
		}
	}

	/**
	 * 获取品牌的完整信息
	 * @param $brandId
	 * @return array|null
	 */
	public static function getFull($brandId) {
		global $db;
		$brandIdin = is_numeric($brandId) ? intval($brandId) : implode(',', $brandId);
		$sql       = "select * from  " . self::_getTableBrand() . " tb, " . self::_getTableCategory() . " c where c.arid = tb.arid and c.arid in($brandIdin)";
		if (is_numeric($brandId)) {
			return $db->getOne($sql);
		} else {
			$full = array();
			$rs   = $db->query($sql);
			while ($r = $db->fetchArray($rs)) {
				$full[$r['arid']] = $r;
			}
			return $full;
		}
	}

	/**
	 * 从缓存中读取列表，根据传过来的id，找出下级列表
	 * @param       $brandid 传入的车系的父ID，即品牌的ID
	 * @param array $condition
	 * @return array
	 */
	public static function manusAndCars($brandid, $condition = array()) {
		global $db;
		$manufact          = array();
		$RANK              = AutoCategory::getCache(AutoCategory::TYPE_RANK);
		$tableAutoCategory = Table::name(Table::NAME_AUTO_CATEGORY);
		$tableCar          = Table::name(Table::NAME_AUTO_RANK_CAR);
		if ($condition) {
			$condition = implode('', (array) __($condition)->map(function ($item) {
				return ' and ' . $item;
			}));
		} else {
			$condition = '';
		}

		$sql  = "select * from $tableAutoCategory c left join $tableCar tc on  c.arid = tc.arid where c.parentid in(select arid from $tableAutoCategory where parentid =$brandid) {$condition}";
		$cars = $db->getAll($sql);

		foreach ($RANK as $id => $rank) {
			if ($rank['parentid'] == $brandid) {
				$manufact[$id]        = $rank; // 这里搞出来是车系
				$manufact['children'] = __($cars)->groupBy('parentid');
			}
		}
		return $manufact;
	}

	/**
	 * 获取车系列表
	 * @param       $brandid 传入的车系的父ID，即品牌的ID
	 * @return array
	 */
	public static function cars($brandid) {
		global $db;
		$tableAutoCategory = Table::name(Table::NAME_AUTO_CATEGORY);
		$tableCar          = Table::name(Table::NAME_AUTO_RANK_CAR);


		$sql  = "select * from $tableAutoCategory c left join $tableCar tc on  c.arid = tc.arid where c.parentid in(select arid from $tableAutoCategory where parentid =$brandid)";
		$cars = $db->getAll($sql);

		return $cars;
	}

	public static function getList($filter, $fields = '*') {
		global $db;
		$tableAutoModel     = Table::name(Table::NAME_AUTO_MODEL);
		$tableAutoCategory  = Table::name(Table::NAME_AUTO_CATEGORY);
		$tableAutoRankBrand = Table::name(Table::NAME_AUTO_RANK_BRAND);
		$condition          = ' 1 ';
		if ($filter['acid']) { // 级别
			$condition .= " and acid in (" . AutoCategoryLevel::get($filter['acid'], 'arrchildid') . ")";
		}
		if ($filter['engine']) { // 引擎|能源
			$condition .= " and enginetype in (" . AutoCategoryEngine::get($filter['engine'], 'arrchildid') . ")";
		}
		$sql = "select distinct(arbid) bid from {$tableAutoModel} where $condition";

		$distinctBrandid = $db->getCol($sql, 'bid');

		if ($distinctBrandid) {
			$sql  = "
				select $fields
				from {$tableAutoCategory} ac,{$tableAutoRankBrand} arb
				where
					ac.arid in (" . implode(',', $distinctBrandid) . ")
					and
					arb.arid = ac.arid";
			$list = array();
			$rs   = $db->query($sql);
			while ($r = $db->fetchArray($rs)) {
				$r['letter']      = strtolower($r['letter']);
				$list[$r['arid']] = $r;
			}
			return $list;
		} else {
			return array();
		}
	}

	public static function sortByLetter($brands) {
		$result = array();
		usort($brands, 'xd_catsort');
		foreach ($brands as $brand) {
			$result[$brand['arid']] = array(
				'brandID'        => $brand['arid'],
				'brandName'      => $brand['letter'] . '-' . $brand['name'],
				'brandletter'    => $brand['letter'],
				'brandShortName' => $brand['name'],
				'brandThumb'     => $brand['brand_thumb']
			);
		}
		$BRANDS = array();
		foreach ($result as $v) {
			$BRANDS[$v['brandletter']][] = $v;
		}
		return $BRANDS;
	}

	public static function getLetterBrands() {
		$RANK = AutoCategory::getCache(AutoCategory::TYPE_RANK);
		$brands = (array) __($RANK)->filter(function($item) {
			return $item['level'] == 0;
		});
		usort($brands, 'xd_catsort');
		$result = array();
		foreach ($brands as $brand) {
			$result[$brand['arid']] = array(
				'brandId'        => $brand['arid'],
				'brandLetter'    => strtoupper($brand['letter']),
				'brandShortName' => $brand['name'],
			);
		}
		$BRANDS = array();
		foreach ($result as $v) {
			if ($v['brandLetter']) {
				$BRANDS[$v['brandLetter']][] = $v;
			}

		}
		return $BRANDS;
	}

	public static function getBrands(){
		$RANK = AutoCategory::getCache(AutoCategory::TYPE_RANK);
		$brands = (array) __($RANK)->filter(function($item) {
			return $item['level'] == 0;
		});
		$return = array();
		foreach($brands as $brand) {
			$return[$brand['arid']] = $brand;
		}
		return $return;

	}


	private static function _getTableCategory() {
		if (!empty(self::$_tableCategory)) {
			return self::$_tableCategory;
		} else {
			self::$_tableCategory = Table::name(Table::NAME_AUTO_CATEGORY);
			return self::$_tableCategory;
		}
	}


	private static function _getTableBrand() {
		if (!empty(self::$_tableBrand)) {
			return self::$_tableBrand;
		} else {
			self::$_tableBrand = Table::name(Table::NAME_AUTO_RANK_BRAND);
			return self::$_tableBrand;
		}
	}
}